package cn.schoolwow.util.module.check.instance.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.lang.reflect.Array;
import java.util.Collection;

public class CheckInstanceNotEmptyFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Object value = flowContext.getData("value");
        String message = (String) flowContext.checkData("message");

        if(null==value){
            throw new IllegalArgumentException(message+"不能为空");
        }
        if(value instanceof String){
            String valueString = (String) value;
            if(valueString.isEmpty()){
                throw new IllegalArgumentException(message+"不能为空");
            }
        }else if(value.getClass().isArray()){
            if(Array.getLength(value)==0){
                throw new IllegalArgumentException(message+"不能为空");
            }
        }else if(value instanceof Collection){
            Collection collection = (Collection) value;
            if(collection.isEmpty()){
                throw new IllegalArgumentException(message+"不能为空");
            }
        }
    }

    @Override
    public String name() {
        return "实例-检查非空数组字段";
    }
}
